package com.itzxx.library_base_ktx.base.activity

import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider
import com.itzxx.library_base_ktx.base.viewmodel.BaseViewModel
import com.itzxx.library_base_ktx.ext.getVmClazz
import com.itzxx.library_base_ktx.ext.util.notNull
import com.itzxx.library_base_ktx.network.manager.NetState
import com.itzxx.library_base_ktx.network.manager.NetworkStateManager
import com.itzxx.library_base_ktx.view.WaitDialog

/**
 * 作者　: hegaojian
 * 时间　: 2019/12/12
 * 描述　: ViewModelActivity基类，把ViewModel注入进来了
 */
abstract class BaseVmActivity<VM : BaseViewModel> : AppCompatActivity() {

    lateinit var mViewModel: VM

    private lateinit var waitDialog: WaitDialog.Builder

    abstract fun layoutId(): Int

    abstract fun initView(savedInstanceState: Bundle?)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initDataBind().notNull({
            setContentView(it)
        }, {
            setContentView(layoutId())
        })
        init(savedInstanceState)
    }

    override fun onResume() {
        super.onResume()
        mViewModel.onResumeViewModel()
    }

    override fun onPause() {
        super.onPause()
        mViewModel.onPauseViewModel()
    }

    override fun onStop() {
        super.onStop()
        mViewModel.onStopViewModel()
    }

    override fun onDestroy() {
        super.onDestroy()
        mViewModel.onDestroyViewModel()
    }

    private fun init(savedInstanceState: Bundle?) {
        mViewModel = createViewModel()
        //databinding和viewmodel连接
        databind2ViewModel()
        //主要用于启动activity
        mViewModel.baseContext.value = this
        //viewmodel创建完成
        mViewModel.onCreateViewModel()
        registerUiChange()
        initView(savedInstanceState)
        createObserver()
        initData()
        NetworkStateManager.instance.mNetworkStateCallback.observe(this) {
            onNetworkStateChanged(it)
        }
    }

    /**
     * 网络变化监听 子类重写
     */
    open fun onNetworkStateChanged(netState: NetState) {}

    /**
     * 创建viewModel
     */
    private fun createViewModel(): VM {
        return ViewModelProvider(this).get(getVmClazz(this))
    }

    /**
     * 创建LiveData数据观察者
     */
    abstract fun createObserver()

    /**
     * 注册UI 事件
     */
    private fun registerUiChange() {
        //基础类默认一下，也可以自定义
        if (isApplyDefaultWaitDialog()) {
            waitDialog = WaitDialog.Builder(this)
        }
        //显示弹窗
        mViewModel.loadingChange.showDialog.observe(this) {
            showLoading(it)
        }
        //关闭弹窗
        mViewModel.loadingChange.dismissDialog.observe(this) {
            dismissLoading()
        }

        //tooast提醒
        mViewModel.toastEventLiveData.observe(this) {
            runOnUiThread {
                Toast.makeText(this, it, Toast.LENGTH_LONG).show()
            }
        }
    }

    /**
     * 将非该Activity绑定的ViewModel添加 loading回调 防止出现请求时不显示 loading 弹窗bug
     * @param viewModels Array<out BaseViewModel>
     */
    protected fun addLoadingObserve(vararg viewModels: BaseViewModel) {
        viewModels.forEach { viewModel ->
            //显示弹窗
            viewModel.loadingChange.showDialog.observe(this) {
                showLoading(it)
            }
            //关闭弹窗
            viewModel.loadingChange.dismissDialog.observe(this) {
                dismissLoading()
            }
        }
    }

    open fun initData() {

    }


    /**
     * 供子类BaseVmDbActivity 初始化Databinding操作
     */
    open fun initDataBind(): View? {
        return null
    }

    open fun isApplyDefaultWaitDialog(): Boolean{
        return false
    }

    open fun databind2ViewModel(){

    }

    open fun showLoading(message: String = "请求网络中..."){
        if (isApplyDefaultWaitDialog()){
            waitDialog.show(message)
        }
    }

    open fun dismissLoading(){
        if (isApplyDefaultWaitDialog()){
            waitDialog.dismiss()
        }
    }
}